<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
<jsp:include page="../inc.jsp"></jsp:include>
<script type="text/javascript" charset="utf-8">
	var datagrid;
	var auth = "${auths}";
	var buttons = [];
	var tyid = "${sessionInfo.type}";
	$(function() {
	    $('form input').bind('keyup', function(event) {/* 增加回车提交功能 */
			if (event.keyCode == '13') {
				datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
			}
		});
		if(auth=="all"){
			buttons.push({text : '增加',iconCls : 'icon-add',handler : function() {append();}});
			buttons.push('-');
			buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
			buttons.push('-');
			buttons.push({text : '编辑',iconCls : 'icon-edit',handler : function() {edit();}});
			buttons.push('-');
			buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
			buttons.push('-');
		}else if(auth!=""){
			var auths = auth.split(",");
			if(auths.length>0){
				for(var i=0;i<auths.length;i++){
					if(auths[i]=="SPGL_SPGL_ADD"){
						buttons.push({text : '增加',iconCls : 'icon-add',handler : function() {append();}});
						buttons.push('-');
					}else if(auths[i]=="SPGL_SPGL_DELETE"){
						buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
						buttons.push('-');
					}else if(auths[i]=="SPGL_SPGL_EDIT"){
						buttons.push({text : '编辑',iconCls : 'icon-edit',handler : function() {edit();}});
						buttons.push('-');
					}
				}
				buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
				buttons.push('-');
			}
		}
		datagrid = $('#datagrid').datagrid({
			url : 'goodsAction!datagrid.html',
			title : '商品列表',
			iconCls : 'icon-save',	
			pagination : true,
			pagePosition : 'bottom',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40 ],
			fit : true,
			fitColumns : false,
			nowrap : false,
			border : false,
			idField : 'id',
			sortName : 'name',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			singleSelect : true,
			frozenColumns : [ [ {
				title : '编号',
				field : 'id',
				width : 80,
				sortable : true,
				checkbox : true
			}] ],
			columns : [ [  {
				title : '商品条码',
				field : 'barcode',
				width : 125,
				sortable : true,
				
			},  {
				title : '款号',
				field : 'type',
				width : 90,
				sortable : true,
			}, {
				title : '商品类别ID',
				field : 'gtid',
				width : 100,
				hidden : true,
				sortable : true,
			},  {
				title : '商品名称',
				field : 'name',
				width : 120,
				sortable : true,
			}, {
				title : '品牌ID',
				field : 'bid',
				width : 100,
				hidden : true,
				sortable : true,
			},  {
				title : '品牌',
				field : 'bname',
				width : 80,
			}, {
				title : '商品类别',
				field : 'gtname',
				width : 90,
				align:'center',
			}, {
				title : '颜色',
				field : 'color',
				width : 60,
				align:'center',
				sortable : true,
			}, {
				title : '尺码',
				field : 'size',
				width : 60,
				align:'center',
				sortable : true,
			},{
				title : '单位',
				field : 'unit',
				width : 50,
				align:'center'
			}, {
				title : '吊牌价',
				field : 'showPrice',
				width : 60,
				align:'center',
				sortable : true,
			}, {
				title : '折扣ID',
				field : 'did',
				width : 80,
				hidden : true,
				sortable : true,
			}, {
				title : '进价',
				field : 'buyPrice',
				width : 60,
				align:'center',
				sortable : true,
			},{
				title : '备注',
				field : 'remark',
				width : 150,
				hidden : true
			} ] ],
			toolbar :buttons,
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
		if(tyid==5||tyid==1){
			$('#datagrid').datagrid('showColumn','buyPrice')
		}else{
			$('#datagrid').datagrid('hideColumn','buyPrice')
		}
		
		var gtid0 = $('#searchForm').find('input[name=gtid]');
		var gtid0Combobox = gtid0.combobox({
			url : '${pageContext.request.contextPath}/spgl/goodsTypeAction!noAuth_combobox.html',
			valueField : 'id',
			textField : 'name',
			multiple : false,
			editable : true,
		});
		
		var bid0 = $('#searchForm').find('input[name=bid]');
		var bid0Combobox = bid0.combobox({
			url : '${pageContext.request.contextPath}/spgl/brandAction!noAuth_combobox.html',
			valueField : 'id',
			textField : 'name',
			multiple : false,
			editable : true,
			onLoadSuccess : function() {
				parent.$.messager.progress('close');
			}
		});
		
		var rid0 = $('#searchForm').find('input[name=rid]');
		var rid0Combobox = rid0.combobox({
			url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
			valueField : 'id',
			textField : 'name',
			multiple : false,
			editable : false,
			onLoadSuccess : function() {
				parent.$.messager.progress('close');
			}
		});

	});

	function edit() {
		var rows = datagrid.datagrid('getSelections');
		if (rows.length == 1) {
			var p = parent.sy.dialog({
				title : '编辑商品',
				href : '${pageContext.request.contextPath}/spgl/goodsAction!goodsEdit.html',
				width : 700,
				height : 225,
				buttons : [ {
					text : '编辑',
					handler : function() {
						var f = p.find('form');
						f.form('submit', {
							url : '${pageContext.request.contextPath}/spgl/goodsAction!edit.html',
							success : function(d) {
								var json = $.parseJSON(d);
								if (json.success) {
									datagrid.datagrid('reload');
									p.dialog('close');
								}
								parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
								});
							}
						});
					}
				} ],
				onLoad : function() {
					var f = p.find('form');
					var gtid = f.find('input[name=gtid]');
					var gtidCombobox = gtid.combobox({
						url : '${pageContext.request.contextPath}/spgl/goodsTypeAction!noAuth_combobox.html',
						valueField : 'id',
						textField : 'name',
						multiple : false,
						editable : false
					});
					var rid = f.find('input[name=rid]');
					var bid = f.find('input[name=bid]');
					
					var bidCombobox = bid.combobox({
						url : '${pageContext.request.contextPath}/spgl/brandAction!noAuth_combobox.html',
						valueField : 'id',
						textField : 'name',
						multiple : false,
						editable : false,
					    onSelect : function(record){
					    if(record.id!=null){
					    $.ajax({
							url : '${pageContext.request.contextPath}/spgl/brandAction!findDiscountNum.html?model.id='+record.id,
							dataType : 'json',
							success : function(d){
								var json = d.obj;
								if(json[0].did!=null){
									f.find('input[name=did]').val(json[0].did);
									f.find('input[name=discountNum]').val(json[0].dname);
								}
								if(json[0].dname!=null){
									var discount = json[0].dname;
									var showPrice = f.find('input[name=showPrice]').val();
									var salePrice  = f.find('input[name=salePrice]').val();	
								if(showPrice>0){
									f.find('input[name=salePrice]').val(parseFloat(discount*0.1*showPrice).toFixed(2));
								}else{
									f.find('input[name=salePrice]').val(0);
								}
								}
							/*	var ridCombobox = rid.combobox({
									url : '${pageContext.request.contextPath}/rkgl/repertoryAction!findByIds.html?model.ids='+json[0].rid1,
									valueField : 'id',
									textField : 'name',
									multiple : false,
									editable : false,
									panelHeight : 'auto',
									onLoadSuccess : function() {
										parent.$.messager.progress('close');
									}
								});*/
							}
							
							});
						}
						}
					});
					
				    var rid = f.find('input[name=rid]');
					var ridCombobox = rid.combobox({
						url : '${pageContext.request.contextPath}/rkgl/repertoryAction!findByIds.html?model.ids='+rows[0].bid,
						valueField : 'id',
						textField : 'name',
						multiple : false,
						editable : false,
						onLoadSuccess : function() {
							parent.$.messager.progress('close');
						}
					});
					
					f.form('load', {
						id : rows[0].id,
						barcode:rows[0].barcode,
						type:rows[0].type,
						name : rows[0].name,
						bid:rows[0].bid,
						gtid:rows[0].gtid,
						color:rows[0].color,
						size:rows[0].size,
						unit:rows[0].unit,
						showPrice:rows[0].showPrice,
						did:rows[0].did,
						discountNum:rows[0].discountNum,
						buyPrice:rows[0].buyPrice,
						rid : rows[0].rid,
						remark:rows[0].remark
					});
				}
			});
		} else if (rows.length > 1) {
			parent.sy.messagerAlert('提示', '同一时间只能编辑一条记录！', 'error');
		} else {
			parent.sy.messagerAlert('提示', '请选择要编辑的记录！', 'error');
		}
	}

	function append() {
		var p = parent.sy.dialog({
			title : '添加商品',
			href : '${pageContext.request.contextPath}/spgl/goodsAction!goodsAdd.html',
			width : 700,
			height : 200,
			buttons : [ {
				text : '添加',
				handler : function(){
					var f = p.find('form');
					f.form('submit', {
						url : '${pageContext.request.contextPath}/spgl/goodsAction!add.html',
						success : function(d) {
							var json = $.parseJSON(d);
							if (json.success) {
								datagrid.datagrid('reload');
								p.dialog('close');
							}
							parent.sy.messagerShow({
								msg : json.msg,
								title : '提示'
							});
						}
					});
				}
			} ],
			onLoad : function() {
				var f = p.find('form');
				var gtid = f.find('input[name=gtid]');
				var gtidCombobox = gtid.combobox({
					url : '${pageContext.request.contextPath}/spgl/goodsTypeAction!noAuth_combobox.html',
					valueField : 'id',
					textField : 'name',
					multiple : false,
					editable : false,
				});
				var rid = f.find('input[name=rid]');
				var bid = f.find('input[name=bid]');
				var bidCombobox = bid.combobox({
					url : '${pageContext.request.contextPath}/spgl/brandAction!noAuth_combobox.html',
					valueField : 'id',
					textField : 'name',
					multiple : false,
					editable : false,
					onSelect : function(record){
						$.ajax({
							url : '${pageContext.request.contextPath}/spgl/brandAction!findDiscountNum.html?model.id='+record.id,
							dataType : 'json',
							success : function(d){
								var json = d.obj;
								if(json[0].did!=null){
									f.find('input[name=did]').val(json[0].did);
									f.find('input[name=discountNum]').val(json[0].dname);
								}
								var showPrice = f.find('input[name=showPrice]').val();
								var salePrice  = f.find('input[name=salePrice]').val();	
								if(json[0].dname!=null){
									var discount = json[0].dname;
									if(showPrice>0){
										f.find('input[name=salePrice]').val(parseFloat(discount*0.1*showPrice).toFixed(2));
									}else{
										f.find('input[name=salePrice]').val(0);
									}
								}else{
									f.find('input[name=salePrice]').val(parseFloat(showPrice).toFixed(2));
								}
								/*var ridCombobox = rid.combobox({
									url : '${pageContext.request.contextPath}/rkgl/repertoryAction!findByIds.html?model.ids='+json[0].rid1,
									valueField : 'id',
									textField : 'name',
									multiple : false,
									editable : false,
									panelHeight : 'auto',
									onLoadSuccess : function() {
										parent.$.messager.progress('close');
									}
								});*/
							}
							
						});
					}
				});
			}
		});
	}
	function remove() {
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.sy.messagerConfirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/spgl/goodsAction!delete.html',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(d) {
							datagrid.datagrid('load');
							datagrid.datagrid('unselectAll');
							parent.sy.messagerShow({
								title : '提示',
								msg : d.msg
							});
						}
					});
				}
			});
		} else {
			parent.sy.messagerAlert('提示', '请勾选要删除的记录！', 'error');
		}
	}
	
	function _search() {
		datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
	}
	
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
	}
</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
		<div data-options="region:'north',border:false,title:'搜索条件'"
			style="height: 140px; overflow: hidden;" align="left">
			<form id="searchForm">
				<table class="tableForm datagrid-toolbar"
					style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">
							商品条码
						</th>
						<td style="width: 220px;">
							<input name="barcode" style="width: 200px;" />
						</td>
						<th style="width: 80px;">
							款&nbsp;&nbsp;&nbsp;&nbsp;号
						</th>
						<td style="width: 220px;">
							<input name="type" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<th style="width: 80px;">
							品&nbsp;&nbsp;&nbsp;&nbsp;牌
						</th>
						<td style="width: 220px;">
							<input name="bid" style="width: 200px;" />
						</td>
						<th style="width: 80px;">
							商品类别
						</th>
						<td style="width: 220px;">
							<input name="gtid" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<th style="width: 80px;">颜&nbsp;&nbsp;&nbsp;&nbsp;色</th>
						<td style="width: 220px;">
							<input name="color" style="width: 200px;" />
						</td>
						<th style="width: 80px;">
							尺&nbsp;&nbsp;&nbsp;&nbsp;码
						</th>
						<td style="width: 220px;">
							<input name="size" style="width: 200px;" />
						</td>
					</tr>
					<tr>
<!--						<th style="width: 80px;">-->
<!--							所属仓库-->
<!--						</th>-->
<!--						<td style="width: 220px;">-->
<!--							<input name="rid" style="width: 200px;" />-->
<!--						</td>-->
						<td colspan="4" style="text-align:center">
							<a href="javascript:void(0);" class="easyui-linkbutton" 
								onclick="_search();">搜索</a><a href="javascript:void(0);"
								class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div data-options="region:'center',border:false"
			style="overflow: hidden;">
			<table id="datagrid"></table>
		</div>

		<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
			<k:security key="SPGL_SPGL_ADD">
				<div onclick="append();" data-options="iconCls:'icon-add'">增加</div>
			</k:security>
			<k:security key="SPGL_SPGL_DELETE">
				<div onclick="remove();" data-options="iconCls:'icon-remove'">删除</div>
			</k:security>
			<k:security key="SPGL_SPGL_EDIT">
				<div onclick="edit();" data-options="iconCls:'icon-edit'">编辑</div>
			</k:security>
		</div>
	</body>
</html>